﻿using System;
using System.Collections.Generic;
using System.Text;
using   System.Runtime.InteropServices; 
namespace TcpLabCommon
{
    /// <summary>
    /// 包类型
    /// </summary>
    public enum PacketType
    {
        /// <summary>
        /// 简单字符串传输
        /// </summary>
        STRING = 1,
        /// <summary>
        /// 通道配置
        /// </summary>
        ACQParametersConfig = 3,
        /// <summary>
        /// 时间设定
        /// </summary>
        TimeSetting = 5,

        /// <summary>
        /// 位移数据包
        /// </summary>
        DisplacementPacket = 11,
        /// <summary>
        /// 加速度数据包
        /// </summary>
        AccelerationPacket = 4,
        /// <summary>
        /// 转速数据包
        /// </summary>
        SpeedPacket = 13,
        /// <summary>
        /// 采样配置
        /// </summary>
        SamplingPacket = 2,
        /// <summary>
        /// 慢速信号包
        /// </summary>
        SlowSignalPacket = 15,
        /// <summary>
        /// 时间上传
        /// </summary>
        TimeUpload = 16,
        
        /// <summary>
        /// 通道配置
        /// </summary>
        ChannelConfig=13,

        /// <summary>
        /// 系统配置
        /// </summary>
        SystemConfig=14

    }




    /// <summary>
    /// 包头
    /// </summary>
    public class NetPacketHead
    {
        /// <summary>
        /// 包头大小
        /// </summary>
        public const Int32 HEAD_SIZE = 3 * 4;

        private Int32 _version = 1;

        /// <summary>
        /// 版本
        /// </summary>
        public Int32 Version
        {
            get { return _version; }
            set { _version = value; }
        }

        private PacketType _pType = PacketType.STRING;

        /// <summary>
        /// 包类型[决定如何解包]
        /// </summary>
        public PacketType PType
        {
            get { return _pType; }
            set { _pType = value; }
        }



        private Int32 _len = 0;
        /// <summary>
        /// 包体长度[决定后面data数据的长度]
        /// </summary>
        public Int32 Len
        {
            get { return _len; }
            set { _len = value; }
        }
    }

    /// <summary>
    /// 网络数据包【Udp建议大小不超过548个字节,Internet标准MTU为576,减去IP数据报头(20字节)和UDP数据包头(8字节)】
    /// </summary>
    public class NetPacket
    {

        NetPacketHead _packetHead;
        /// <summary>
        /// 包头
        /// </summary>
        public NetPacketHead PacketHead
        {
            get { return _packetHead; }
            set { _packetHead = value; }
        }

        private object _data = null;
        /// <summary>
        /// 包体，根据封包类型决定包体数据类型
        /// STRING:包体为String
        /// BINARY:包体为Byte[]
        /// COMPLEX:包体为可序列化的对象
        /// </summary>
        public object Data
        {
            get { return _data; }
            set { _data = value; }
        }
    }

    /// <summary>
    /// 传输的文件
    /// </summary>
    public class NetFile
    {
        private string _fileName = string.Empty;

        public string FileName
        {
            get { return _fileName; }
            set { _fileName = value; }
        }
        private Byte[] _content;

        public Byte[] Content
        {
            get
            {
                if (_content == null)
                    _content = new Byte[] { };
                return _content;
            }
            set { _content = value; }
        }
    }
}
